package com.elegantmall.controller;

import cn.dev33.satoken.util.SaResult;
import com.elegantmall.pojo.CartPojo;
import com.elegantmall.pojo.GoodsPojo;
import com.elegantmall.pojo.OrderPojo;
import com.elegantmall.service.OrderServicelmpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.dev33.satoken.SaManager.log;

@RestController
@CrossOrigin
@RequestMapping("/api/order")

public class OrderController {

    @Autowired
    OrderServicelmpl orderService;


   @PostMapping("/create")
    public SaResult createOrder(
           @RequestParam String goodsId,
           @RequestParam String sellerName,
           @RequestParam int number
   ) {
        try {
            String result = orderService.createOrder(goodsId,sellerName,number);
            return SaResult.ok(result);
        } catch (IllegalArgumentException e) {
            return SaResult.error(e.getMessage());
        } catch (Exception e) {
            return SaResult.error("创建订单失败");
        }
    }
/*

    // 根据订单ID查询订单
    @GetMapping("/search/{order_id}")
    public SaResult getOrderById(@PathVariable String order_id) {
        try {
            OrderPojo order = orderService.getOrderById(order_id);
            return SaResult.data(order);
        } catch (IllegalArgumentException e) {
            return SaResult.error(e.getMessage());
        } catch (Exception e) {
            return SaResult.error("查询订单失败");
        }
    }
*/

    /*// 根据商品ID查询订单列表
    @GetMapping("/goods/{goods_id}")
    public SaResult getOrdersByGoodsId(@PathVariable String goods_id) {
        try {
            List<OrderPojo> orders = orderService.getOrdersByGoodsId(goods_id);
            return SaResult.data(orders);
        } catch (Exception e) {
            return SaResult.error("查询订单列表失败");
        }
    }
*/
    // 根据卖家名称(double类型)查询订单列表
    @GetMapping("/seller/{seller_name}")
    public SaResult getOrdersBySellerName(@PathVariable String seller_name) {
        try {
            List<OrderPojo> orders = orderService.getOrdersBySellerName(seller_name);

            return SaResult.data(orders);
        } catch (Exception e) {
            return SaResult.error("查询订单列表失败");
        }
    }

    /*// 更新订单信息
    @PostMapping("/update")
    public SaResult updateOrder(@RequestBody OrderPojo order) {
        try {
            String result = orderService.updateOrder(order);
            return SaResult.ok(result);
        } catch (IllegalArgumentException e) {
            return SaResult.error(e.getMessage());
        } catch (Exception e) {
            return SaResult.error("更新订单失败");
        }
    }
*/
    // 删除订单
    @PostMapping("/delete")
    public SaResult deleteOrder(@RequestParam String order_id) {
        try {
            String result = orderService.deleteOrder(order_id);
            return SaResult.ok(result);
        } catch (IllegalArgumentException e) {
            return SaResult.error(e.getMessage());
        } catch (Exception e) {
            return SaResult.error("删除订单失败");
        }
    }

}
